package com.hl.card.credit.pub.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.hl.card.credit.pub.common.exception.CheckException;


public class FormValid {
	
	
	/**
	 * 参数验证
	 * @param value
	 * @param msg
	 * @throws ServiceException 
	 */
	public static void validNull(String value,String msg) throws CheckException{
		if(StringUtils.isBlank(value)){
			throw new CheckException("2",msg);
		}
	}
	
	/**判断是否修改成功,
	 * @param value
	 * @param msg
	 */
	public static void validUpdate(int value,String msg){
		if(value==0){
			throw new CheckException("2",msg);
		}
	}
	/**
	 * 字段为空,请求非法
	 * @param value
	 * @param msg
	 * @throws CheckException
	 */
	public static void validIllegality(String value,String msg) throws CheckException{
		if(StringUtils.isBlank(value)){
			throw new CheckException("3",msg);
		}
	}
	
	/**
	 * 超时
	 * @param value
	 * @param msg
	 * @throws CheckException
	 */
	public static void validOutTime(String value,String msg) throws CheckException{
		if(StringUtils.isBlank(value)){
			throw new CheckException("10030",msg);
		}
	}
	
	/**
	 * 判断对象是否为空
	 * @param obj
	 * @param msg
	 * @throws CheckException
	 */
	public static void validNull(Object obj,String msg) throws CheckException{
		if(null==obj){
			throw new CheckException("2",msg);
		}
	}
	public static void validEmpty(Map map,String msg) throws CheckException{
		if(map == null || map.size() == 0){
			throw new CheckException("2",msg);
		}
	}
	
	
	/**
	 * 参数验证
	 * @param value
	 * @param msg
	 * @throws ServiceException 
	 */
	public static void validEmpty(Object[] values,String msg) throws CheckException{
		if(values == null || values.length < 1){
			throw new CheckException("2",msg);
		}
	} 
	
	
	
	
	/**
	 * 参数验证
	 * @param value
	 * @param msg
	 * @throws ServiceException 
	 */
	public static void validNull(Integer value,String msg) throws CheckException{
		if(value == null){
			throw new CheckException("2",msg);
		}
	} 
	
	/**
	 * 参数验证
	 * @param msg
	 * @param value
	 * @return 返回该参数
	 * @throws CheckException
	 */
	public static Integer validNull(String msg,Integer value) throws CheckException{
		if(value == null){
			throw new CheckException("2",msg);
		}
		return value;
	} 
	
	
	/**
	 * 判断字符串长度
	 * @param value
	 * @param minLen
	 * @param maxLen
	 * @param msg
	 * @throws ServiceException
	 */
	public static void  validLen(String value,Integer maxLen,String msg) throws CheckException{
		int len =StringUtils.isNotBlank(value) ? value.length() :  0; 
		if(len > maxLen){
			throw new CheckException("2",msg);
		}
	}
	
	/**
	 * 判断字符串长度
	 * @param value
	 * @param minLen
	 * @param maxLen
	 * @param msg
	 * @throws ServiceException
	 */
	public static void  validLen(String value,Integer minLen,Integer maxLen,String msg) throws CheckException{
		int len =StringUtils.isNotBlank(value) ? value.length(): 0; 
		if( len < minLen ||  len > maxLen){
			throw new CheckException("2",msg);
		}
	}
	
	/**
	 * 验证是否数字
	 * @param value
	 * @param minLen
	 * @param maxLen
	 * @param msg
	 * @throws ServiceException
	 */
	public static void  validNumber(String value,String msg) throws CheckException{
		if(!NumberUtils.isNumber(value)){
			throw new CheckException("2",msg);
		}
	}
		
	
	/**
	 * 验证是否int 数字
	 * @param value
	 * @param minLen
	 * @param maxLen
	 * @param msg
	 * @throws ServiceException
	 */
	public static void  validInt(String value,String msg) throws CheckException{
		try{
			Integer.parseInt(value);
		}catch(Exception e){
			throw new CheckException("2",msg);
		}
	}
	
	
	
	/**
	 * 验证是否Float 数字
	 * @param value
	 * @param minLen
	 * @param maxLen
	 * @param msg
	 * @throws ServiceException
	 */
	public static void  validFloat(String value,String msg) throws CheckException{
		try{
			Float.parseFloat(value);
		}catch(Exception e){
			throw new CheckException("2",msg);
		}
	}
	
	/**
	 * 正在表达式
	 * @param value
	 * @param regex
	 * @param code
	 * @param msg
	 */
	public static void validValue(String value, String regex, String code,String msg) throws CheckException{
		if (StringUtils.isNotBlank(regex)) {
			if (!ReglexUtils.regex(regex, value)) {
				throw new CheckException(code, msg);
			}
		}
	}
	
	/**
	 * 比较两个值是否相同
	 * @param value
	 * @param msg
	 * @throws ServiceException 
	 */
	public static void validEqual(String value1,String value2,String msg) throws CheckException{
		if(!value1.equals(value2)){
				throw new CheckException("2",msg);
		}
	} 
	//验证邮箱
	public static boolean validEmail(String email)
	{
		String chek = "^([a-zA-Z0-9]+[_|\\-|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\-|\\.]?)*[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3})+$";
		 Pattern regex = Pattern.compile(chek);    
		 Matcher matcher = regex.matcher(email);    
		 boolean isMatched = matcher.find();  
		if(email != null && !"".equals(email) && isMatched)
		{
			return true;
		}
		return false;
	}
	
	private static boolean isMatch(String regex, String orginal){  
        if (orginal == null || orginal.trim().equals("")) {  
            return false;  
        }  
        Pattern pattern = Pattern.compile(regex);  
        Matcher isNum = pattern.matcher(orginal);  
        return isNum.matches();  
    }
	
	public static boolean isPositiveDecimal(String orginal){  
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);  
    }
	
	public static boolean isPositiveInteger(String orginal) {  
        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);  
    }
	
	//验证数字
	public static boolean validPositiveNumber(String number){
		return isPositiveDecimal(number) || isPositiveInteger(number);
		
	}
	
	/**
	 * 抛出异常，指定异常代码和信息
	 * @param num 
	 * <li> 20001 无参与资格
	 * @param msg
	 * @throws CheckException
	 */
	public static void errCodeMsg(String code,String msg) throws CheckException{
				throw new CheckException(code,msg);
	} 

}

